// pages/forum-list/forum-list.js
const app = getApp();
const util = require('../../utils/util.js');

Page({
  data: {
    forumList: [],
    searchKeyword: '',
    publisher: '',
    orderBy: ''
  },

  onLoad() {
    this.loadForumList();
  },

  onShow() {
    // 从详情页返回时刷新列表
    if (this.data.forumList.length > 0) {
      this.loadForumList();
    }
  },

  loadForumList() {
    app.showLoading();
    const userInfo = wx.getStorageSync('userInfo');
    const userId = userInfo ? userInfo.id : null;

    // 构建请求参数
    const requestData = {
      page: 1,
      size: 100
    };
    
    if (userId) {
      requestData.userId = userId;
    }
    
    if (this.data.searchKeyword) {
      requestData.title = this.data.searchKeyword;
      requestData.keywords = this.data.searchKeyword;
    }
    
    if (this.data.publisher) {
      requestData.publisher = this.data.publisher;
    }
    
    if (this.data.orderBy) {
      requestData.orderBy = this.data.orderBy;
    }

    app.request({
      url: '/forum/posts',
      method: 'GET',
      data: requestData
    }).then(res => {
      if (res.code === 200 || res.success) {
        const list = ((res.data && res.data.list) || res.data || []).map(item => ({
          ...item,
          image: this.getForumImage(item.images),
          description: this.truncateText(item.description || item.content, 100),
          createTime: util.formatDateTime(item.createTime)
        }));
        this.setData({ forumList: list });
      }
    }).catch(err => {
      app.showToast('加载失败');
      console.error(err);
    }).finally(() => {
      app.hideLoading();
    });
  },

  getForumImage(images) {
    if (!images) return '';
    if (typeof images === 'string') {
      const imageArray = images.split(',');
      return imageArray[0] || '';
    }
    return '';
  },

  truncateText(text, length) {
    if (!text) return '';
    if (text.length <= length) return text;
    return text.substring(0, length) + '...';
  },

  // 搜索输入
  onSearchInput(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 搜索
  handleSearch() {
    this.loadForumList();
  },

  // 按发布者筛选
  filterByPublisher(e) {
    const publisher = e.currentTarget.dataset.publisher;
    this.setData({
      publisher: publisher
    });
    this.loadForumList();
  },

  // 按排序方式筛选
  filterByOrder(e) {
    const order = e.currentTarget.dataset.order;
    this.setData({
      orderBy: order
    });
    this.loadForumList();
  },

  viewDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/forum-detail/forum-detail?id=${id}`
    });
  },

  goPublish() {
    const userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateTo({
          url: '/pages/login/login'
        });
      }, 1500);
      return;
    }
    wx.navigateTo({
      url: '/pages/forum-publish/forum-publish'
    });
  }
});


